package cn.edu.swpu.cins.u_chain.frontend.service.Works.Impl;

import cn.edu.swpu.cins.u_chain.frontend.dao.WorksDao.WorksDao;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.Works.WorksView;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.Works.WorksViewDetail;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.Works.WorksViewList;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.Works.WorksInfo;
import cn.edu.swpu.cins.u_chain.frontend.service.Works.WorksService;
import cn.edu.swpu.cins.u_chain.utils.EXCELUtil;
import cn.edu.swpu.cins.u_chain.utils.JedisAdapter;
import cn.edu.swpu.cins.u_chain.utils.RedisKey;
import cn.edu.swpu.cins.u_chain.utils.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class WorksServiceImpl implements WorksService {

    @Autowired
    private WorksDao worksDao;
    @Autowired
    private UploadFile uploadFile;
    @Autowired
    private JedisAdapter jedisAdapter;

    @Override
    public int addWorks(WorksInfo worksInfo) {
        return worksDao.insertWorks(worksInfo);
    }

    @Override
    public int updateWorksVideo(int worksId, MultipartFile video) throws IOException {
        String path = uploadFile.upload(video, worksId, "video");
        return worksDao.updateWorksVideoUrl(worksId, path);

    }

    @Override
    public int insertWorksPrintScreen(int worksId, MultipartFile[] images) throws IOException {
        System.out.println(images.length);
        return uploadPrintScreen(worksId, images);
    }

    @Override
    public WorksViewList selectAllWorks() {
        List<WorksView> worksInfos = worksDao.selectAllWorks();
        return null;
    }

    @Override
    public List<WorksViewDetail> selectAllWorksDetail(List<Integer> worksIds) {
        List<WorksViewDetail> worksViewDetails = worksDao.selectWorksDetail(worksIds);
        return worksViewDetails;
    }

    @Override
    public void downLoadExcel(List<Integer> worksIds,HttpServletResponse response) {
        List<Integer> worksIdList = new ArrayList<>();
        worksIdList.add(1);
        worksIdList.add(2);
        List<WorksViewDetail> worksViewDetails = worksDao.selectWorksDetail(worksIdList);
        setClickNumAndLikeNum(worksViewDetails); //设置点击量和喜欢数
        EXCELUtil.exportExcel(worksViewDetails, "作品联系信息表", "优橙作品", WorksViewDetail.class, "UChain（优橙中国）作品模块报表.xls", response);
    }

    public void setClickNumAndLikeNum(List<WorksViewDetail> worksViewDetails) {
        worksViewDetails.stream().forEach(worksViewDetail -> {
            int worksId = worksViewDetail.getWorksId();
            String clickNumKey = RedisKey.getWorksClineNum(worksId);
            int clickNum = Integer.parseInt(jedisAdapter.get(clickNumKey));
            String worksLikeNumKey = RedisKey.getWorksLike(worksId);
            int worksLikeNum = jedisAdapter.smenber(worksLikeNumKey).size();
            worksViewDetail.setClickNum(clickNum);
            worksViewDetail.setLikeCount(worksLikeNum);
        });
    }

    @Override
    public void likeWorks(int worksId, int userId) {
        String worksLikeKey = RedisKey.getWorksLike(worksId);
        jedisAdapter.sadd(worksLikeKey, String.valueOf(userId));
        String likeWorksKey = RedisKey.getLikeWorks(userId);
        jedisAdapter.sadd(likeWorksKey, String.valueOf(worksId));
    }

    @Override
    public void unLikeWorks(int worksId, int userId) {
        String worksLikeKey = RedisKey.getWorksLike(worksId);
        jedisAdapter.srem(worksLikeKey, String.valueOf(userId));
        String likeWorksKey = RedisKey.getLikeWorks(userId);
        jedisAdapter.srem(likeWorksKey, String.valueOf(worksId));
    }

    public int uploadPrintScreen(int worksId, MultipartFile[] images) throws IOException {
        for (MultipartFile image:images) {
            String path = uploadFile.upload(image, worksId, "printScreen");
            return worksDao.updateWorksPrintScreenUrl(worksId, path);
        }
        return 1;
    }
}
